package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.ChannelMapper;
import com.heima.admin.service.ChannelService;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.model.admin.dtos.WmChannelPageReqDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ChannelServiceImpl extends ServiceImpl<ChannelMapper, WmChannel> implements ChannelService {

    @Autowired
    private ChannelMapper channelMapper;

    @Autowired
    private ApArticleMapper articleMapper;

    /**
     * 频道--保存新增频道
     * @param wmChannel
     * @return
     */
    @Override
    public ResponseResult saveChannel(WmChannel wmChannel) {

        //核实当前参数是否为null,并且频道名称不能为null
        if (wmChannel != null && wmChannel.getName() != null) {
            //查询频道表所有数据
            List<WmChannel> list = list();
            //遍历集合,核实当前新增频道是否已存在
            for (WmChannel channel : list) {
                if (wmChannel.getName().equals(channel.getName())) {
                    return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
                }
            }
            //不存在则添加保存并返回正确信息;
            save(wmChannel);
            return ResponseResult.okResult(wmChannel);
        } else {
            //为null则返回错误信息缺少参数
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
    }

    /**
     * 频道--根据id删除频道
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteChannel(Integer id) {

        //检查参数是否为null并且大于0
        if (id != null && id > 0) {
            //根据id查询,获取频道对象
            WmChannel wmChannel = channelMapper.selectById(id);
            //检查当前对象是否存在
            if (wmChannel!=null){
                //检查当前对象状态是否为0
                if (!wmChannel.getStatus()){
                    channelMapper.deleteById(id);
                    return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
                }
                return ResponseResult.errorResult(AppHttpCodeEnum.CHANNEL_ACTIVE);
            }
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }

    /**
     * 频道--名称模糊分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult pageChannel(WmChannelPageReqDto dto) {
        //1. 检查分页参数
        dto.checkParam();

        //2. 构建分页参数
        IPage<WmChannel> page = new Page(dto.getPage(), dto.getSize());

        //3. 构建查询的条件
        LambdaQueryWrapper<WmChannel> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        //可以按照状态进行精确查找（1：启用   true           0：禁用   false）
        if(dto.getStatus() != null){
            lambdaQueryWrapper.eq(WmChannel::getStatus, dto.getStatus());
        }

        //按照频道名称模糊查询
        if(StringUtils.isNotEmpty(dto.getName())){
            lambdaQueryWrapper.like(WmChannel::getName, dto.getName());
        }
        //按照创建时间倒序查询
        lambdaQueryWrapper.orderByDesc(WmChannel::getCreatedTime);

        //4. 分页查询
        page(page, lambdaQueryWrapper);

        // 封装返回结果
        PageResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int)page.getTotal());

        responseResult.setData(page.getRecords());

        return responseResult;
    }

    /**
     * 频道--修改频道
     *- 点击编辑后可以修改频道
     * - 如果频道被引用则不能禁用
     * @param wmChannel
     * @return
     */
    @Override
    public ResponseResult updateChannel(WmChannel wmChannel) {

        //获取当前对象的状态是否是禁用
        if (!wmChannel.getStatus()){
            //查询当前频道下是否存在引用的文章
            LambdaQueryWrapper<ApArticle> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(ApArticle::getChannelId,wmChannel.getId());
            List<ApArticle> apArticles = articleMapper.selectList(wrapper);
            //如果存在,不能修改状态为禁用
            if (apArticles!=null&&apArticles.size()>0){
                return ResponseResult.errorResult(AppHttpCodeEnum.CHANNEL_CITED);
            }
        }
        //如果状态是启用或者频道下不存在文章,可以修改
        updateById(wmChannel);
        return ResponseResult.okResult(wmChannel);
    }
}
